//
//  LHXEditImageViewController.m
//  Etwo
//
//  Created by 林浩翔 on 16/6/20.
//  Copyright © 2016年 林浩翔. All rights reserved.
//

#import "LHXEditImageViewController.h"
#import "LHXCutImageView.h"
#import "LHXbutton.h"
#import "LHXBtnMenusViewController.h"
#import "LHXpresentVC.h"
#import "LHXanimationModel.h"

@interface LHXEditImageViewController ()

/**放置背景图片*/
@property (weak,nonatomic)UIImageView *BGImageView;
/**放置按钮的数组*/
@property (strong, nonatomic)NSMutableArray *btnArray;
/**放置按钮文字的数组*/
@property (strong, nonatomic)NSMutableArray *btnTextArray;
/**保存按钮*/
@property (weak, nonatomic)LHXbutton *saveBtn;

@end

@implementation LHXEditImageViewController


#pragma mark --懒加载
- (NSMutableArray *)btnArray{
    if (_btnArray == nil) {
        _btnArray = [NSMutableArray array];
    }
    return _btnArray;
}
- (NSMutableArray *)btnTextArray{
    if (_btnTextArray == nil) {
        _btnTextArray = [NSMutableArray array];
    }
    return _btnTextArray;
}

#pragma mark --生命周期
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.BGImageView.image = [LHXchoose shareChoose].chooseImage;
    
    [self.cutImageView removeFromSuperview];
    
    self.cutImageView = [LHXchoose shareChoose].cutImageView;
    
    [self.mainView addSubview:self.cutImageView];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    [self showButtonAnimation];
}



- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    
    for (LHXbutton *btn in self.btnArray) {
        
        [UIView animateWithDuration:0.25 animations:^{
            
            btn.frame = CGRectMake(0,10, btnWH, btnWH);
            
            btn.alpha = 0;
        }];
    }
}


- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor blackColor];
    
    [self setShowImageView];
    
    [self setButton];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveBtnEnableNO) name:@"LHXChooseImageGagin" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveBtnEnableYES) name:@"LHXeditImage" object:nil];
    
    if([LHXchoose shareChoose].saveBtnEnble){
        [[NSNotificationCenter defaultCenter] postNotificationName:@"LHXChooseImageGagin" object:self];
    }
    
}
//
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    NSLog(@"%s",__func__);
}

#pragma mark --设置view画面
- (void)setShowImageView{
    //设置放置选择图片的view
    UIView *mainmainView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    
    self.mainmainView = mainmainView;
    
    [self.view addSubview:mainmainView];
    
    //设置放置选择图片的view
    UIView *poliView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    
    self.poliView = poliView;
    
    [self.mainmainView addSubview:poliView];
    //设置背景图片
    UIImageView *bgImageView = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    
    bgImageView.image = [LHXchoose shareChoose].chooseImage;
    
    bgImageView.contentMode = UIViewContentModeCenter;
    
    self.BGImageView = bgImageView;
    
    [poliView addSubview:bgImageView];
    //设置毛玻璃
    
//    UIVisualEffectView *visualEffectView;
//    
//    visualEffectView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]];
//    
//    [visualEffectView setFrame:[UIScreen mainScreen].bounds];
//    
//    [poliView addSubview:visualEffectView];
    
    UIImageView *temp = [[UIImageView alloc]initWithFrame:self.view.bounds];
    
    temp.image = [UIImage imageNamed:@"login_register_background"];
    
    temp.alpha = 0.99;
    
    [poliView addSubview:temp];
    
    //设置放置选择图片的view
    UIView *mainView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    
    self.mainView = mainView;
    
    [self.mainmainView addSubview:mainView];
    
    
    if (([LHXchoose shareChoose].cutImageView)) {
        self.cutImageView = [LHXchoose shareChoose].cutImageView;
    }else{
        LHXCutImageView *cutImageView = [LHXCutImageView cutImageWith:[LHXchoose shareChoose].chooseImage];
        
        self.cutImageView = cutImageView;
        
        [LHXchoose shareChoose].cutImageView = cutImageView;
    }
    
    [self.mainView addSubview:self.cutImageView];
}
//设置放置按钮的scrollView
- (void)setButton {
    
    UIScrollView *buttonView = [[UIScrollView alloc]init];
    
    CGFloat btnSpace = ([UIScreen mainScreen].bounds.size.width - (MaxBtnCount * btnWH))/ (MaxBtnCount - 1) ;
    
    buttonView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height - btnWH - 30, [UIScreen mainScreen].bounds.size.width, btnWH + 40);
    
    buttonView.contentSize = CGSizeMake((btnWH + btnSpace) * self.btnItemArray.count - btnSpace, btnWH + 40);
    
    buttonView.showsHorizontalScrollIndicator = NO;
    
    buttonView.showsVerticalScrollIndicator = NO;
    
    self.btnScrollView = buttonView;
    
    [self.view addSubview:buttonView];
    
    [self setButtonWithNum:self.btnItemArray.count andSpace:btnSpace];
}


- (void)setButtonWithNum:(NSInteger)num andSpace:(CGFloat)btnSpace{
    
    for (int i = 0 ; i < num; i++) {
        
        LHXbutton *btn = [LHXbutton btn];
        //设置tag是为了出场动画
        btn.tag = i;
        
        btn.alpha = 0;
        
        btn.item = self.btnItemArray[i];
        
        btn.btnSpace = btnSpace;
        
        if ([btn.btnTextTag isEqualToString:@"保存"]) {
            self.saveBtn = btn ;
            [LHXchoose shareChoose].saveBtnEnble = self.saveBtn.enabled;
            
        }
        
        btn.frame = CGRectMake(0,0, btnWH, btnWH);
        
        [btn addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
        //添加到显示的scrollView中
        [self.btnScrollView addSubview:btn];
        //添加到按钮数组中
        [self.btnArray addObject:btn];
        
        UILabel *btnlabel = [[UILabel alloc]init];
        
        btnlabel.frame = CGRectMake(((btnWH + btn.btnSpace)*(btn.tag)),55, btnWH, 30);;
        
        btnlabel.text = btn.btnTextTag;
        
        btnlabel.textAlignment = NSTextAlignmentCenter;
        
        btnlabel.textColor = [UIColor whiteColor];
        
        btnlabel.font = [UIFont systemFontOfSize:13];
        
        [self.btnTextArray addObject:btnlabel];
        
        [self.btnScrollView addSubview:btnlabel];
        
    }
}


- (void)BtnClick:(LHXbutton *)btn{
    
}

#pragma mark --打开相册读取图片
- (void)getPicture{
    
    UIImagePickerController *pickVC = [[UIImagePickerController alloc] init];
    //设置照片来源
    pickVC.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    //只要实现代理方法,必须得要手去关闭控制器
    pickVC.delegate = self;
    //跳转到系统相册控制器
    [self presentViewController:pickVC animated:YES completion:nil];
}

//当选择照片完毕时调用
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    //获取点中选取的照片,用UIimage保存
    UIImage *image = info[UIImagePickerControllerOriginalImage];
    //将获取到的图片用于别的地方
    [LHXchoose shareChoose].chooseImage = image;
    
    [self.cutImageView removeFromSuperview];
    
    LHXCutImageView *temp = [LHXCutImageView cutImageWith:[LHXchoose shareChoose].chooseImage];
    
    [LHXchoose shareChoose].cutImageView = temp;
    //将图片显示保存到成员属性上,添加到屏幕上
    self.cutImageView = temp;
    
    [self.mainView addSubview:self.cutImageView];
    //手动关闭控制器
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"LHXChooseImageGagin" object:self];
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

#pragma mark --控制保存按钮开关
- (void)saveBtnEnableNO{
    self.saveBtn.enabled = NO;
}
- (void)saveBtnEnableYES{
    self.saveBtn.enabled = YES;
}

#pragma mark --按钮动画出场
- (void)showButtonAnimation{
    for (LHXbutton *btn in self.btnArray) {
        
        [UIView animateWithDuration:0.2 animations:^{
            
            btn.frame = CGRectMake(((btnWH + btn.btnSpace)*(btn.tag)),0, btnWH, btnWH);
            
            btn.alpha = 1;
        }];
    }
    
//    for (UILabel *label in self.btnTextArray) {
//        [UIView animateWithDuration:0.2 animations:^{
//            
//            label.frame = CGRectMake(((btnWH + btn.btnSpace)*(btn.tag)),10, btnWH, btnWH);
//            
//            label.alpha = 1;
//        }];
//    }
}


#pragma mark --控制转场
//用来告诉系统谁来负责进行转场弹出控制器
- (nullable UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source{
    
    
    return [[LHXpresentVC alloc]initWithPresentedViewController:presented presentingViewController:presenting];
}


//用来告诉系统谁来负责弹出时候的动画
//当弹出控制器的时候调用
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
    //1.创建自定义的对象
    LHXanimationModel *showDelegate = [[LHXanimationModel alloc]init];
    showDelegate.presented = YES;
    return showDelegate;
}
//用来告诉系统谁来负责销毁控制器时候的动画
//当销毁控制器的时候调用
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
    LHXanimationModel *showDelegate = [[LHXanimationModel alloc]init];
    showDelegate.presented = NO;
    return showDelegate;
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
